package lms.view;

import lms.entity.Book;
import lms.service.BorrowService;
import lms.service.OperationService;
import lms.service.impl.BookServiceImpl;
import lms.service.impl.BorrowServiceImpl;
import lms.utils.VerificationInput;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

/**
 * @description: 图书管理功能
 * @author: Grape_Pip
 * @time: 2021/06/21
 */
public class ManageBookView {
    public Scanner scan = new Scanner(System.in);
    public OperationService operationService = new BookServiceImpl();
    public BorrowService borrowService = new BorrowServiceImpl();
    boolean res = false;

    /**
     * @return
     * @Description //TODO  图书管理操作选择
     * @Param []
     **/
    public ManageBookView() throws ParseException {
        bookOperation();//调用图书管理操作
    }

    /**
     * @return void
     * @Description //TODO  管理图书的具体实现
     * @Param []
     **/
    public void bookOperation() throws ParseException {
        bookManageShow();
        System.out.println("请输入您要选择的操作：");
        String bOption = scan.next();
        switch (bOption) {
            case "1":
//                查询
                List<Book> bookslist = operationService.selectedAllInfo();
//                循环输出
                System.out.println("| 查询结果为：");
                if (bookslist != null && !bookslist.isEmpty()) {
                    for (Book books : bookslist) {
                        System.out.println(books);
                    }
                } else {
                    System.out.println("图书库空空如也~~~");
                    System.out.println();
                }
                bookOperation();
                break;
            case "2":
//                删除 前需要判断是否有人借阅这本书
                System.out.println("请输入需要删除的图书ISBN：");
                int bid;
                while (!scan.hasNextInt()) {
                    String stu = scan.next();
                    System.out.println("请输入纯数字(⊙o⊙)！");
                    System.out.println("请重新输入ISBN吖：");
                }
                bid = scan.nextInt();
                boolean result = borrowService.selectedByBid(bid);
                if (result) {
                    System.out.println("此书籍已借出，不可进行删除！");
                } else {
                    res = operationService.deletedById(bid);
                    if (res) {
                        System.out.println("删除成功！");
                    } else {
                        System.out.println("无此图书信息，删除失败！");
                    }
                }
                bookOperation();
                break;
            case "3":
//                增加
//                输入需要增加的图书信息
                System.out.println("请输入图书编号ISBN：");
                int bId;
                while (!scan.hasNextInt()) {
                    String stu = scan.next();
                    System.out.println("请输入纯数字(⊙o⊙)！");
                    System.out.println("请重新输入吖：");
                }
                bId = scan.nextInt();
//                输入的图书编号需要验证是否已经存在
                Book bk = (Book) operationService.selectedById(bId);
                if (bk != null) {
                    System.out.println("该图书编号已存在！");
                } else {
                    Book bookInfo = scannerBookInfo();
                    bookInfo.setBookISBN(bId);
                    res = operationService.insertedInfo(bookInfo);
                    if (res) {
                        System.out.println("插入成功!");
                    } else {
                        System.out.println("插入失败!");
                    }
                }
                bookOperation();
                break;
            case "4":
//                更新 图书编号不变，其他变
                System.out.println("请输入您需要修改的图书的编号：");
                int bookid;
                while (!scan.hasNextInt()) {
                    String stu = scan.next();
                    System.out.println("请输入纯数字(⊙o⊙)！");
                    System.out.println("请重新输入吖：");
                }
                bookid = scan.nextInt();
                Book ubk = scannerBookInfo();
                ubk.setBookISBN(bookid);
                res = operationService.updatedById(bookid, ubk);
                if (res) {
                    System.out.println("更新成功!");
                } else {
                    System.out.println("更新失败!");
                }
                bookOperation();
                break;
            case "0":
                new ManageView().mOperation();//上一层
            default:
                System.out.println("无此操作哦~");
                bookOperation();
                break;
        }
    }


    /**
     * @return lms.entity.Book
     * @Description //TODO  控制台更新图书信息的book对象
     * @Param []
     **/
    public Book scannerBookInfo() throws ParseException {
        Book bk = new Book();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Scanner scan = new Scanner(System.in);
        boolean flag = false;

        System.out.println("》请输入图书名称：");
        bk.setBookName(scan.next());
        System.out.println("》请输入图书作者：");
        bk.setBookAuthor(scan.next());
        System.out.println("》请输入图书出版社：");
        bk.setBookpublish(scan.next());
        do {
            System.out.println("》请输入图书出版日期（yyyy-MM-dd）：");
            String date = scan.next();
            if (VerificationInput.isDate(date)) {
                bk.setBookPublishDate(dateFormat.parse(date));
                flag = false;
            } else {
                System.out.println("请按照要求输入日期吖~");
                flag = true;
            }
        } while (flag);
        do {
            System.out.println("》请输入图书库存:");
            int stock;
            while (!scan.hasNextInt()) {
                //没有这一行的话会导致无限循环
                String userAge = scan.next();
                System.out.println("非法输入" + userAge);
                System.out.println("请重新输入：");
            }//当是数字时循环结束
            stock = scan.nextInt();
            if (stock > 0 && stock <= 100) {
                bk.setBookStock(stock);
                flag = false;
            } else {
                System.out.println("请正确输入此值吖！！！(0,50]");
                flag = true;
            }
        } while (flag);

        System.out.println("》请输入图书类型:");
        bk.setBookType(scan.next());//类型
        return bk;
    }

    /**
     * @return void
     * @Description //TODO  图书管理功能展示
     * @Param []
     **/
    public void bookManageShow() {
        System.out.println("^========图书馆管理系统 v2.0========^");
        System.out.println("<========     图书管理     ========>");

        System.out.println("|---------- 1、查询图书 -----------|");
        System.out.println("|---------- 2、删除图书 -----------|");
        System.out.println("|---------- 3、增加图书 -----------|");
        System.out.println("|---------- 4、更新图书 -----------|");
        System.out.println("|---------- 0、返回上一层 -----------|");
        System.out.println();
    }
}
